<!-- 防止页面数据重复定义,模板数据统一在此 -->
<!--:
    var statusList = dicttag.getDictList('schedule_status_type');
    var statusJson = commontag.toJson(statusList);
-->
<!-- schedule form content -->
<div class="well dialog-well">

    <div class="widget-body no-padding">

        <form id="schedule-form" action="${ctx}/tools/schedule/update" method="post" class="smart-form">

            <fieldset>
                <div class="hide">
                    <input type="hidden" name="id"> 　
                </div>

                <div class="row">
                    <section class="col col-6">
                        <label class="label">任务名称</label>
                        <label class="input state-disabled">
                            <i class="icon-append fa fa-key"></i>
                            <input type="text" name="name" readonly="readonly">
                            <b class="tooltip tooltip-bottom-right">任务的名称,不可修改</b>
                        </label>
                    </section>
                    <section class="col col-6">
                        <label class="label">任务组</label>
                        <label class="input state-disabled">
                            <i class="icon-append fa fa-tasks"></i>
                            <input type="text" name="group" readonly="readonly">
                            <b class="tooltip tooltip-bottom-right">任务的组名称,不可修改</b>
                        </label>
                    </section>
                </div>
                <section>
                    <label class="label">任务类型</label>
                    <label class="input state-disabled">
                        <i class="icon-append fa fa-paw"></i>
                        <input type="text" name="jobClass" disabled="disabled">
                        <b class="tooltip tooltip-bottom-right">任务的执行类,不可修改</b>
                    </label>
                </section>

                <div class="row">
                    <section class="col col-6">
                        <label class="label">任务开始时间</label>
                        <label class="input">
                            <i class="icon-append fa fa-calendar"></i>
                            <input id="startDate" name="startDate">
                            <b class="tooltip tooltip-bottom-right">任务的开始时间，默认初始化任务的时刻</b>
                        </label>
                    </section>
                    <section class="col col-6">
                        <label class="label">任务结束时间</label>
                        <label class="input">
                            <i class="icon-append fa fa-calendar"></i>
                            <input id="endDate" name="endDate" placeholder="不选择则一直重复执行">
                            <b class="tooltip tooltip-bottom-right">任务的结束时间，可以不设置</b>
                        </label>
                    </section>
                </div>

                <div class="row">
                    <section class="col col-6">
                        <label class="label">下次计划执行时间</label>
                        <label class="input state-disabled">
                            <i class="icon-append fa fa-calendar-plus-o"></i>
                            <input type="text" name="jobRuntimeEntity.nextFireTime" disabled="disabled">
                            <b class="tooltip tooltip-bottom-right">任务下次执行的时间</b>
                        </label>
                    </section>
                    <section class="col col-6">
                        <label class="label">上次执行时间</label>
                        <label class="input state-disabled">
                            <i class="icon-append fa fa-calendar-minus-o"></i>
                            <input type="text" name="jobRuntimeEntity.previousFireTime" disabled="disabled">
                            <b class="tooltip tooltip-bottom-right">任务上次执行的时间，可能未开始</b>
                        </label>
                    </section>
                </div>
                <section>
                    <label class="label">任务状态</label>
                    <label class="input state-disabled">
                        <i class="icon-append fa fa-recycle"></i>
                        <input type="text" name="jobRuntimeEntity.status" disabled="disabled">
                        <b class="tooltip tooltip-bottom-right">任务的执行状态，不可修改</b>
                    </label>
                </section>

                <section>
                    <label class="label">触发器类型</label>
                    <label class="select" rel="tooltip" data-placement="bottom"
                           data-original-title="普通类型的使用定长的时间间隔,表达式模式使用cron表达式">
                        <select name="type">
                            <option value="1">普通模式</option>
                            <option value="2">表示式模式</option>
                        </select><i></i>
                    </label>
                </section>
                <div id="schedule-unit" class="row">
                    <section class="col col-6">
                        <label class="label">间隔类型</label>
                        <label class="select" rel="tooltip" data-placement="bottom"
                               data-original-title="间隔的类型">
                            <select name="unitType">
                                <option value="1">秒</option>
                                <option value="2">分</option>
                                <option value="3">小时</option>
                                <option value="4">天</option>
                            </select><i></i>
                        </label>
                    </section>
                    <section class="col col-6">
                        <label class="label">间隔时长</label>
                        <label class="input">
                            <i class="icon-append fa fa-random"></i>
                            <input type="text" name="interval">
                            <b class="tooltip tooltip-bottom-right">间隔的长度</b>
                        </label>
                    </section>
                </div>
                <section id="schedule-cron" style="display: none">
                    <label class="label">Cron表达式</label>
                    <label class="input">
                        <i class="icon-append fa fa-sheqel"></i>
                        <input type="text" name="cron" readonly="readonly">
                        <b class="tooltip tooltip-bottom-right">cron表达式设置</b>
                    </label>
                </section>

                <section>
                    <label class="label">任务描述</label>
                    <label class="textarea">
                        <i class="icon-append fa fa-comment"></i>
                        <textarea rows="4" name="description"></textarea>
                    </label>
                </section>

                <section>
                    <label class="checkbox"><input type="checkbox" name="history" value="1">
                        <i></i>任务是否保存执行历史记录</label>
                </section>

            </fieldset>
            <footer>
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-save"></i>
                    确定
                </button>
                <button type="button" class="btn btn-default" onclick="pageRedirct();">
                    <i class="fa fa-reply"></i>
                    取消
                </button>
            </footer>
        </form>

    </div>
</div>
<!-- end schedule form content -->

<script type="text/javascript">

    /* DO NOT REMOVE : GLOBAL FUNCTIONS!
     *
     * pageSetUp(); WILL CALL THE FOLLOWING FUNCTIONS
     *
     * // activate tooltips
     * $("[rel=tooltip]").tooltip();
     *
     * // activate popovers
     * $("[rel=popover]").popover();
     *
     * // activate popovers with hover states
     * $("[rel=popover-hover]").popover({ trigger: "hover" });
     *
     * // activate inline charts
     * runAllCharts();
     *
     * // setup widgets
     * setup_widgets_desktop();
     *
     * // run form elements
     * runAllForms();
     *
     ********************************
     *
     * pageSetUp() is needed whenever you load a page.
     * It initializes and checks for all basic elements of the page
     * and makes rendering easier.
     *
     */
    pageSetUp();

    /*
     * ALL PAGE RELATED SCRIPTS CAN GO BELOW HERE
     */
    // PAGE RELATED SCRIPTS
    // pagefunction
    var pagefunction = function () {

        var $scheduleForm = $("#schedule-form");
        $scheduleForm.validate({
            // Rules for form validation
            rules: {
                // 为了不污染html代码,验证统一在js中
                name: {
                    required: true,
                    maxlength: 50
                },
                interval: {
                    required: true,
                    digits: true,
                    min: 1
                },
                cron: {
                    required: true
                },
                startDate: {
                    required: true
                },
                description: {
                    maxlength: 250
                }
            },

            // Messages for form validation,use default
            messages: {
                // 不覆盖使用默认js多语言文件
            },
            submitHandler: function (form) {
                // 遮罩
                var loadIndex = parent.layer.load(0, {shade: 0.4});

                $(form).ajaxSubmit({
                    dataType: "json",
                    success: function (data) {
                        parent.layer.close(loadIndex);

                        if (data && data.status == 'success') {
                            parent.messageBox(data.message, "success");
                            pageRedirct();
                        } else {
                            parent.messageBox(data.message, "error");
                        }
                        // 显示
                        parent.layer.closeAll('page');
                    },
                    error: function () {
                        parent.layer.close(loadIndex);
                        parent.messageBox("修改调度任务错误!", "error");
                    }
                });
                // always return false to prevent standard browser submit and page navigation
                return false;
            },
            // Do not change code below
            errorPlacement: function (error, element) {
                error.insertAfter(element.parent());
            }
        });

        // 初始化日期选择器
        var start = {
            format: 'YYYY-MM-DD hh:mm:ss',
            min: '1984-12-10 00:00:00', //设定最小日期
            max: '2099-12-10 23:59:59', //最大日期
            istime: true,
            istoday: true,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
            }
        };
        var end = {
            format: 'YYYY-MM-DD hh:mm:ss',
            min: '1984-12-10 00:00:00', //设定最小日期
            max: '2099-12-10 23:59:59', //最大日期
            istime: true,
            istoday: true,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        // 设置下拉
        $scheduleForm.find('input[name=startDate]').on('click', function () {
            laydate(start);
        });
        $scheduleForm.find('input[name=endDate]').on('click', function () {
            laydate(end);
        });
        // 设置初始值
        $scheduleForm.find('input[name=startDate]').val('');
        $scheduleForm.find('input[name=endDate]').val('');

        /**
         * 设置触发类型
         */
        $scheduleForm.find('select[name=type]').on('change', function () {
            if ($(this).val() == 1) {
                $('#schedule-unit').show();
                $('#schedule-cron').hide();
                $scheduleForm.find('input[name=cron]').val('0 0 1 * * ?');
            } else {
                $('#schedule-unit').hide();
                $('#schedule-cron').show();
                $scheduleForm.find('input[name=interval]').val('5');
                $scheduleForm.find('select[name=unitType]').val(1);
            }
        });

        /**
         * 根据类型获取类型label
         * @param value 类型value
         */
        function getLabel(value) {
            var dicts = eval('${statusJson!""}');
            // 设置默认显示type
            var label = value;
            if (dicts) {
                $.each(dicts, function (i, dict) {
                    if (dict['value'] == value) {
                        label = dict['label'];
                        return false;
                    }
                });
            }
            return label;
        }

        /**
         * 初始化form
         */
        function initForm() {
            var entity = JSON.parse('${entity}');
            $scheduleForm.find('input[name=id]').val(entity['id']);
            $scheduleForm.find('input[name=name]').val(entity['name']);
            $scheduleForm.find('input[name=group]').val(entity['group']);
            $scheduleForm.find('input[name=jobClass]').val(entity['jobClass']);
            $scheduleForm.find('input[name=startDate]').val(entity['startDate']);
            $scheduleForm.find('input[name=endDate]').val(entity['endDate']);
            $scheduleForm.find('input[name="jobRuntimeEntity.nextFireTime"]').val(entity["jobRuntimeEntity"]['nextFireTime']);
            $scheduleForm.find('input[name="jobRuntimeEntity.previousFireTime"]').val(entity["jobRuntimeEntity"]['previousFireTime']);
            $scheduleForm.find('input[name="jobRuntimeEntity.status"]').val(getLabel(entity["jobRuntimeEntity"]['status']));
            $scheduleForm.find('textarea[name=description]').val(entity['description']);
            if (entity['history'] == true) {
                $scheduleForm.find('input[name=history]').prop('checked', 'checked');
            }


            if (entity['cron'] || entity['interval'] == 0) {
                // cron
                $scheduleForm.find('input[name=cron]').val(entity['cron']);
                $scheduleForm.find('select[name=type]').val(2).trigger('change');
            } else {
                $scheduleForm.find('select[name=unitType]').val(entity['unitType']);
                $scheduleForm.find('input[name=interval]').val(entity['interval']);
                // interval
                $scheduleForm.find('select[name=type]').val(1).trigger('change');
            }
        }

        initForm();

        // 增加cron监听
        $('#schedule-cron').find('input').on('click', function () {
            cronSel($scheduleForm.find('input[name=cron]').val());
        });

        /**
         * 保存和取消之后页面到列表
         */
        function pageRedirct() {
            var treeObj = $.fn.zTree.getZTreeObj("scheduleTree");
            var node = treeObj.getNodeByParam("id", $scheduleForm.find('input[name=group]').val());
            if (node) {
                treeObj.selectNode(node);
                $("#" + node.tId + "_a").click();
            }
        }
        // 注册
        window.pageRedirct = pageRedirct;

        laydate.reinit();
    };

    // destroy generated instances
    // pagedestroy is called automatically before loading a new page
    // only usable in AJAX version!
    var pagedestroy = function () {
        $(".spinner").spinner("destroy");
        window.pageRedirct = undefined;
    };
    // end destroy

    // load related plugins
    loadScript("/static/js/plugin/laydate/laydate.js", function () {
        loadScript("/static/js/plugin/jquery-form/jquery.form.js", pagefunction);
    });

</script>